package com.nuonuo.util.tmp;

import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author duan
 * @version 1.0
 * @date 2023/12/28 14:05
 */
public class SynchronousQueueTest {

    private static final AtomicInteger acount = new AtomicInteger(1);

    private static final ArrayBlockingQueue queue = new ArrayBlockingQueue(8);


    public static void main(String[] args) throws InterruptedException {


        new ThreadPoolExecutor(
                4,
                4,
                0L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4),
                new ThreadPoolExecutor.AbortPolicy())
                .execute(new PrintHandler());

        new ThreadPoolExecutor(
                10,
                10,
                10L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.AbortPolicy())
                .execute(() -> {
                    while(true) {
                        try {
                            queue.put(new Random().nextInt());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private static class PrintHandler implements Runnable {

        @Override
        public void run() {
            while(true) {
                try {
                    System.out.println(Thread.currentThread().getName() + "##########" + queue.take());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
